package io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * write = output = 写
 * read = input = 读
 *
 * Created by cuihao on 2017/10/11.
 */
public class IOtest {
    public static void main(String[] args) {

        String aa = "sadfslghsfdsnkfms";


        String bb = readFile("D:/文本.txt");
//        readBufferFile("D:/文本.txt");
        writeBufferFile(bb, "D:/文本1.txt");
    }


    private static String readFile(String filePath) {
        FileInputStream inputStream = null;
        try {
            // 字节流，外部文件 > 变量
            inputStream = new FileInputStream(filePath);
            byte[] arr = new byte[inputStream.available()];
            inputStream.read(arr);
            String result = new String(arr);
            System.out.println(result);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private static String readBufferFile(String filePath) {
        FileInputStream inputStream = null;
        try {
            // 字节流，外部文件 > 缓存 > 变量
            inputStream = new FileInputStream(filePath);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            byte[] arr = new byte[inputStream.available()];
            bufferedInputStream.read(arr);
            String result = new String(arr);
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private static String writeBufferFile(String str, String filePath) {

        FileOutputStream outputStream = null;
        try {
            // 字节流，变量 > 缓存 > 外部文件
            outputStream = new FileOutputStream(filePath);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
            byte[] bytes = str.getBytes();
            bufferedOutputStream.write(bytes);
            // 缓存 > 外部文件
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private void aa() {
        File f = new File("f:/hah/exportCompany.txt");
        f.getParentFile().mkdirs();
        f.mkdirs();
        System.out.println(f.exists());
        try {
            if (!f.exists()) {
                f.createNewFile();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

/*
 *
 * InputStream只有read方法。inputStream.read(byte)。将数据读到byte里
 * OutputStream只有write方法。outputStream.write(byte)。将byte里的数据写到outputStream里
 *
 * InputStreamReader只有read方法
 * OutputStreamWrite只有write方法
 *
 *
 *
 * */